package com.ruoyi.service;

import com.ruoyi.dto.ResponseDTO;
import com.ruoyi.mapper.StoreMapper;
import com.ruoyi.pojo.Message;
import com.ruoyi.pojo.MessageList;
import com.ruoyi.pojo.Store;
import com.ruoyi.pojo.User;
import com.ruoyi.povo.MessageListVo;
import com.ruoyi.povo.MessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StoreServiceImpl implements StoreService{
@Autowired
private StoreMapper storeMapper;
    @Override
    public ResponseDTO getStoreById(String id) {
        Store store = storeMapper.getStoreById(id);
        return ResponseDTO.success(store);
    }

    @Override
    public ResponseDTO preserveStore(Store store) {
        int res=storeMapper.preserveStore(store);
        if(res>0){
            return ResponseDTO.success("保存成功");
        }
        else{
            return ResponseDTO.error("保存失败");
        }
    }

    @Override
    public ResponseDTO changeStore(String id, String status) {
        int res=storeMapper.changeStore(id,status);
        if(res>0){
            return ResponseDTO.success("修改成功");
        }
        else{
            return ResponseDTO.error("修改失败");
        }
    }

    @Override
    public ResponseDTO getUserById(String id) {
        User user=storeMapper.getUserById(id);
        return ResponseDTO.success(user);
    }

    @Override
    public ResponseDTO storeMessageList(MessageListVo messageListVo) {
        int count= storeMapper.checkFromUidExists(messageListVo.getFromUid());
        if(count>0){
            storeMapper.deleteMessageByFromUid(messageListVo.getFromUid());
        }

        int res= storeMapper.storeMessageList(messageListVo);
        if(res>0){
            return ResponseDTO.success("存储成功");
        }
        else{
            return ResponseDTO.error("存储失败");
        }
    }

    @Override
    public ResponseDTO getMessageList(String id) {
        List<MessageList> messageLists=storeMapper.getMessageList(id);
        return ResponseDTO.success(messageLists);
    }

    @Override
    public ResponseDTO readMessageList(String id) {
        int res=storeMapper.readMessageList(id);
        if(res>0){
            return ResponseDTO.success("读取成功");
        }
        else{
            return ResponseDTO.error("读取信息失败");

        }
    }

    @Override
    public ResponseDTO storeMessage(MessageVo messageVo) {
        int res=storeMapper.storeMessage(messageVo);
        if(res>0){
            return ResponseDTO.success("存储消息成功");
        }
        else{
            return ResponseDTO.error("存储消息失败");
        }
    }

    @Override
    public ResponseDTO getMessageById(String fromUid,String toUid) {
        List<Message> messageVos=storeMapper.getMessageById(fromUid,toUid);
        return ResponseDTO.success(messageVos);
    }

    @Override
    public ResponseDTO getMessageByFromId(String id) {
        Message message=storeMapper.getMessageByFromId(id);
        return ResponseDTO.success(message);
    }

    @Override
    public ResponseDTO readAllMessage(String fromUid,String toUid) {
        int res=storeMapper.readAllMessage(fromUid,toUid);
        if(res>0){
            return ResponseDTO.success("读取所有客户端成功");
        }
        else{
            return ResponseDTO.error("读取所有客户端失败");
        }
    }

    @Override
    public ResponseDTO readAllMessageS(String id) {
        int res=storeMapper.readAllMessageS(id);
        if(res>0){
            return ResponseDTO.success("读取所有客服消息成功");
        }
        else{
            return ResponseDTO.error("读取所有客服消息失败");
        }
    }
}
